<template>
  <div class="FareWellDialog">
    <el-dialog
      title="礼厅信息维护"
      :visible.sync="$parent.DialogFareWell"
      v-dialogDrag
      width="85%"
      @close="closeDialog()"
    >
      <el-form
        :model="copyFareWellInfo.farewellMourn"
        :rules="rules"
        :inline="true"
        label-width="120px"
        ref="FareWell"
        :disabled="saveFlag"
      >
        <el-row :gutter="5">
          <el-form-item label="告別厅类型：" prop="hallTypeCode">

            <el-select
              v-model="copyFareWellInfo.farewellMourn.hallTypeCode"
              @change="changeTypeCodeAsRuleItem(copyFareWellInfo.farewellMourn.hallTypeCode)"
              :disabled="ProcessStateFareWellOld==3||((copyFareWellInfo.farewellMourn.charges.businessCharge.asSettled>0?true:false))"
              placeholder="请选择告別厅类型"
              style="width: 120px;"
            >
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="(item,index) in ruleItems"
                :key="index"
                :label="item.dataText"
                :value="item.dataCode"

              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间：" prop="appointmentInTime">
            <el-date-picker
              v-on:change="changeOutTime()"
              v-model="copyFareWellInfo.farewellMourn.appointmentInTime"
              format="yyyy-MM-dd HH:mm"
              type="datetime"
              :picker-options="pickerOptions0"
              @blur="loadChargeWithRule()"
              prop="farewellMourn.appointmentInTime"
              :disabled="ProcessStateFareWellOld==3||(copyFareWellInfo.farewellMourn.charges.businessCharge.asSettled > 0)? true:false"
              placeholder="选择日期时间"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间：">
            <el-date-picker
              v-model="copyFareWellInfo.farewellMourn.appointmentOutTime"
              format="yyyy-MM-dd HH:mm"
              type="datetime"
              :picker-options="pickerOptions0"
              @blur="loadChargeWithRule()"
              :disabled="ProcessStateFareWellOld==3||(copyFareWellInfo.farewellMourn.charges.businessCharge.asSettled > 0)? true:false"
              placeholder="选择日期时间"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="礼厅名称：" prop="hallCode">
            <el-input v-model="selectSave.hallName" :disabled="true">
              <span slot="append" style="cursor: pointer;" @click="select()" >选择</span>
            </el-input>
          </el-form-item>
          <el-form-item label="告别费用：">
            <el-input
              v-model="copyFareWellInfo.farewellMourn.charges.businessCharge.itemCharge"
              @change="changRemark(copyFareWellInfo.farewellMourn.charges.businessCharge.itemCharge)"
              type="number"
              :readonly="( null !=this.ruleItem.asFixPrice && this.ruleItem.asFixPrice==0)?false:true"
            ></el-input>
          </el-form-item>
          <el-form-item label="告别状态：" prop="processStateCode">
            <el-select
              v-model="copyFareWellInfo.farewellMourn.processStateCode"
              placeholder="请选择"
              onkeydown="return (event.keyCode!=8)"
              @change="changeFareWellProcessStateCode(copyFareWellInfo.farewellMourn.processStateCode)"
              :disabled="ProcessStateFareWellOld==3"
              style="width: 120px;"
            >
              <el-option
                v-for="(item,index) in  hallStatus"
                :key="index"
                :label="item.dataText"
                :value="item.dataCode"
                :disabled="item.disableState>0?true:false"
              />
            </el-select>
          </el-form-item>
        </el-row>

        <el-row :gutter="5">
          <el-form-item label="规则描述：">
            <el-input
              type="textarea"
              v-model="copyFareWellInfo.farewellMourn.ruleRemark"
              :readonly="true"
              style="width: 808px;"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="备注：">
            <el-input
              type="textarea"
              v-model="copyFareWellInfo.farewellMourn.remark"
              style="width: 808px;"
            ></el-input>
          </el-form-item>
        </el-row>
        <div class="tablewrapper">
          <div class="table">
            <div class="title">
              <span>所有物品、服务</span>
              <!--<el-input v-model="search" placeholder="搜索物品/服务" style="width: 200px;float: right;"></el-input>-->
            </div>
            <el-table
              :data="copyGoodServices"
              :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
              border
              style="width: 99%;"
            >
              <el-table-column
                align="center"
                prop="itemName"
                label="收费项目"
                sortable
                min-width="120"
                column-key="serviceItemId"
              ></el-table-column>
              <el-table-column label="操作" align="center" width="100">
                <template slot-scope="{$index}">
                  <el-button
                    @click.native.prevent="AddRow($index,copyGoodServices)"
                    type="text"
                    size="small"
                  >追加</el-button>
                </template>
              </el-table-column>
              <el-table-column prop="itemPrice" align="right" label="单价" min-width="80">
                <template slot-scope="{row}">
                  <input
                    class="edit-cell"
                    v-if="row.asFixPrice !=1"
                    type="number"
                    onkeyup="value= (value.match(/^\d*(\.?\d{0,2})/g)[0]) || null"
                    v-model="row.itemPrice"
                  />
                  <label class="edit-cell" v-else-if="row.asFixPrice ==1">{{row.itemPrice}}</label>
                </template>
              </el-table-column>

              <el-table-column prop="itemNumber" align="right" label="数量" min-width="50">
                <template slot-scope="{row}">
                  <input
                    class="edit-cell"
                    v-model="row.itemNumber"
                    type="number"
                    onkeyup="value= (value.match(/^\d*(\.?\d{0,2})/g)[0]) || null"
                  />
                </template>
              </el-table-column>
              <el-table-column prop="itemUnit" align="center" label="单位" min-width="50"></el-table-column>
              <el-table-column prop="itemCharge" align="right" label="总金额" min-width="80">
                <template
                  slot-scope="{row}"
                  v-if="Number(row.itemPrice)>=0 &&Number(row.itemNumber)>=0&&Number(row.discountCharge)>=0"
                >{{Number((row.itemPrice.match(/^\d+(?:.\d{0,2})?/))*(row.itemNumber.match(/^\d+(?:.\d{0,2})?/))).toFixed(2)}}</template>
              </el-table-column>
              <el-table-column prop="discountCharge" align="right" label="优惠" min-width="50">

                <template slot-scope="{row}">
                  <input
                    class="edit-cell"
                    v-model="row.discountCharge"
                    onkeyup="value= (value.match(/^\d*(\.?\d{0,2})/g)[0]) || null"
                  />
                </template>
              </el-table-column>
                <el-table-column align="center" prop="itemSurplusNum" label="物品剩余" ></el-table-column>

            </el-table>
            <!--<p class="tablecont">单价、数量、优惠三个字段值允许工作人员直接更改，更改完成后可通过“追加”按钮进行费用追加。</p>-->
          </div>
          <div class="table">
            <h3 class="title">已追加物品、服务</h3>
            <el-table
              :data="copyFareWellInfo.farewellMourn.charges.goodsServices"
              :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
              border
              style="width: 99%;"
            >
              <el-table-column align="center" prop="itemName" label="收费项目" min-width="100"></el-table-column>
              <el-table-column prop="itemPrice" align="right" label="单价" min-width="80"></el-table-column>
              <el-table-column prop="itemNumber" align="right" label="数量" min-width="50"></el-table-column>
              <el-table-column prop="itemUnit" align="center" label="单位" min-width="50"></el-table-column>
              <el-table-column prop="itemCharge" align="right" label="项目金额" min-width="80"></el-table-column>
              <el-table-column prop="discountCharge" align="right" label="优惠" min-width="50"></el-table-column>

              <el-table-column prop="benefitCharge" align="right" label="减免" min-width="50"></el-table-column>
              <el-table-column prop="asSettled" align="center" label="结算状态" width="80">
                <template slot-scope="{row}">
                  <span v-if="row.asSettled==0">未结算</span>
                  <span v-else-if="row.asSettled==1">已结算</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="{row,$index}">
                  <el-button
                    v-if="row.asSettled==0"
                    @click.native.prevent="deleteRow($index,copyFareWellInfo.farewellMourn.charges.goodsServices)"
                    type="text"
                    size="small"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--<p class="tablecont">此区域费用信息不允许更改，仅未结算的费用可以进行删除操作。-->
            <!--无论是新追加的费用还是删除的费用，均在保存操作后生效。-->
            <!--</p>-->
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: right">
        <el-button type="primary" @click="Save()" :disabled="saveFlag">保 存</el-button>
        <el-button type="primary" plain @click="$parent.DialogFareWell = false">取 消</el-button>
      </div>
      <el-dialog title="礼厅名称" :visible.sync="centerDialogVisible" width="60%" center>
        <div class="wrapper">
          <div
            class="box-card"
            v-if="farewellHalls.length>0"
            v-for="(n,m) in farewellHalls"
            :key="m"
          >
            <div
              v-if="n.disableState==1"
              class="item repair"
              :class="{selected:n.flag}"
              @click.stop="mournSelect(n,m)"

            >
              <h2 v-if="n.hallCode">礼厅名称：{{n.hallName}}</h2>
              <h2 v-if="n.hallCode">礼厅类型：{{n.hallType}}</h2>
            </div>
            <div
              v-if="n.disableState==0 && !n.businessId"
              class="item free"
              :class="{selected:n.flag}"
              @click.stop="mournSelect(n,m)"
              @dblclick.stop="showSelectName()"
            >
              <h2 v-if="n.hallCode">礼厅名称：{{n.hallName}}</h2>
              <h2 v-if="n.hallCode">礼厅类型：{{n.hallType}}</h2>
            </div>
            <div
              v-if="n.disableState==0 && n.businessId>0"
              class="item sale"
              :class="{selected:n.flag}"
              @click.stop="mournSelect(n,m)"

            >
              <h2 v-if="n.hallCode">礼厅名称：{{n.hallName}}</h2>
              <h2 v-if="n.hallType">礼厅类型：{{n.hallType}}</h2>
              <h2 v-if="n.deadName">逝者姓名：{{n.deadName}}</h2>
              <h2 v-if="n.businessCode">业务编号：{{n.businessCode}}</h2>

              <div style="width:280px" v-if="n.appointmentList && n.appointmentList.length==1" >
                <span v-if="n.appointmentInTime">{{n.appointmentInTime}}</span>-<span v-if="n.appointmentOutTime">{{n.appointmentOutTime}}</span>
              </div>
              <el-popover
                v-if="n.appointmentList && n.appointmentList.length>1"
                placement="top-start"
                title="任务列表"
                width="250"
                trigger="hover"
              >
                <div
                  v-if="n.appointmentList && n.appointmentList.length>0"
                  class="list"
                  v-for="item in n.appointmentList"
                >
                  <h2>{{item}}</h2>
                </div>
                <el-button type="text" style="font-size:16px;color:#606266;" slot="reference" margin-top="5px">
                  任务列表：
                  <span v-if="n.appointmentInTime">{{n.appointmentInTime}}</span> ~
                  <span v-if="n.appointmentOutTime">{{n.appointmentOutTime}}</span>
                </el-button>
              </el-popover>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer" style="text-align: right">
          <el-button type="primary" @click="showSelectName()">确 定</el-button>
          <el-button type="primary" plain @click="centerDialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import * as Type from "@/api/service";
import * as mournService from "@/api/mournService";
import { validPhone } from "@/utils/validate";
import { dataFormate } from "@/utils/index";
import { toFixed } from "@/utils/tofixed";

export default {
  name: "FareWellDialog",
  props: {
    FareWellInfo: {
      type: Object,
      default: function () {
        return {
          farewellMourn: {
            id: 0,
            businessCode: "",
            businessType: "",
            hallTypeCode: "",
            hallId: 0,
            processStateCode: "",
            appointmentInTime: "",
            appointmentOutTime: "",
            version: 0,
            calculateRule: "",
            charges: {
              businessCharge: {
                id: 0,
                serviceItemId: 0,
                itemPrice: 0,
                itemNumber: 0,
                itemUnit: "",
                itemCharge: 0,
                discountCharge: 0,
                benefitCharge: 0,
                realityCharge: 0,
                asSettled: 0,
                version: 0,
              },
              goodsServices: [],
            },
          },
        };
      },
    },
    DialogFareWell: {
      type: Boolean,
      default: false,
    },
    FareWellAddFlag: {
      //true为新增
      type: Boolean,
      default: false,
    },
    ruleItems: {
      type: Array,
    }, //礼厅状态
    hallStatus: {
      type: Array,
    },
    ProcessStateFareWellOld: {
      //老状态
      type: Number,
    },
  },
  data() {
    return {
      dictionary: {},
      rules: {
        "farewellMourn.hallTypeCode": [
          { required: true, message: "请选择类型", trigger: "blur" },
        ],
        "farewellMourn.appointmentInTime": [
          { required: true, message: "请选择开始时间", trigger: "blur" }, //这里需要用到全局变量
        ],

        " farewellMourn.hallCode": [
          { required: true, message: "请选择礼厅名称", trigger: "blur" },
        ],
      },
      formLabelWidth: "120px",
      //开始时间小于等于今天可选
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      saveFlag: false,
      copyGoodServices: [],
      centerDialogVisible: false,
      farewellHalls: [], //根据礼厅类型加载礼厅信息
      selectname: {}, //选中礼厅
      selectSave: {}, //点击确定时选中礼厅显示在界面上
      copyFareWellInfo: this.FareWellInfo,
      ruleItem: {}, //收费规则
      onpresscTime: 0, //  阻止短时间内连续点击
    };
  },
  created() {
    this.$store
      .dispatch("user/getdictionary")
      .then((res) => {
        //获取字典数据
        this.dictionary = res;
        this.$set(this.dictionary, res);
      })
      .catch(() => console.log("promise catch err"));
  },
  watch: {
    "$parent.DialogFareWell"() {
      this.copyFareWellInfo = this.FareWellInfo;
      this.copyGoodServices = JSON.parse(
        JSON.stringify(this.FareWellInfo.serviceItems.use_in_mourn_ceremony)
      ); //复制
      if (!this.FareWellAddFlag) {
        // console.log("inTime", this.copyFareWellInfo.farewellMourn.appointmentInTime)
        if (
          typeof this.copyFareWellInfo.farewellMourn.appointmentOutTime ==
            "string" &&
          this.copyFareWellInfo.farewellMourn.appointmentOutTime.indexOf("-") >
            -1
        ) {
          this.copyFareWellInfo.farewellMourn.appointmentOutTime = this.copyFareWellInfo.farewellMourn.appointmentOutTime.replace(
            /-/g,
            "/"
          );
        }
        if (
          typeof this.copyFareWellInfo.farewellMourn.appointmentInTime ==
            "string" &&
          this.copyFareWellInfo.farewellMourn.appointmentInTime.indexOf("-") >
            -1
        ) {
          this.copyFareWellInfo.farewellMourn.appointmentInTime = this.copyFareWellInfo.farewellMourn.appointmentInTime.replace(
            /-/g,
            "/"
          );
        }
      }
      this.$parent.DialogFareWell = this.DialogFareWell;
      if (
        this.copyFareWellInfo.farewellMourn.hallCode &&
        !this.FareWellAddFlag
      ) {
        //如果名字存在并且为编辑状态

        this.selectSave.hallCode = this.copyFareWellInfo.farewellMourn.hallCode;
        this.selectSave.hallId = this.copyFareWellInfo.farewellMourn.hallId;
        this.selectSave.hallTypeCode = this.copyFareWellInfo.farewellMourn.hallTypeCode;
        this.selectSave.hallName=this.copyFareWellInfo.farewellMourn.hallName;
      }
      if (this.$parent.DialogFareWell == false) {
        this.$nextTick(() => {
          this.$refs.FareWell.resetFields();
        });
        this.selectname = {};
        this.selectSave = {};
      }
    },
  },
  methods: {
    //当类型改变时，查找适用计算规则
    changeTypeCodeAsRuleItem(code) {
      if (!code) {
        this.$message("code值无效");
        return;
      }
      //判断并选择费用
      if (this.ruleItems) {
        for (let i = 0; i < this.ruleItems.length; i++) {
          let item = this.ruleItems[i];
          //选中code'
          if (item.dataCode == code) {
            //服务项目id
            this.copyFareWellInfo.farewellMourn.charges.businessCharge.serviceItemId =
              item.serviceItemId;
            //服务项目价格
            this.copyFareWellInfo.farewellMourn.charges.businessCharge.itemPrice =
              item.itemPrice;
            //规则编码
            this.copyFareWellInfo.farewellMourn.ruleCode = item.ruleCode;
            //规则数量
            this.copyFareWellInfo.farewellMourn.ruleNumber = item.itemNumber;
            //计量单位
            this.copyFareWellInfo.farewellMourn.ruleUnit = item.itemUnit;

            //规则描述
            this.copyFareWellInfo.farewellMourn.ruleRemark = item.ruleDescribe;

            // 计算规则编码
            this.ruleItem.ruleCode = item.ruleCode;
            // 计算规则单价
            this.ruleItem.itemPrice = item.itemPrice;
            // 计算规则参数
            this.ruleItem.ruleParams = item.ruleParams;
            // 计算规则是否议价
            if(item.ruleParams){
              let objRule=JSON.parse(item.ruleParams);
              if( null !=objRule.asFixPrice && objRule.asFixPrice==0){
                this.ruleItem.asFixPrice=0;
              }else{
                this.ruleItem.asFixPrice=1;
              }

            }else{
              this.ruleItem.asFixPrice=1;
            }
            this.loadChargeWithRule();
            break;
          }
        }
      }
    },
    //计算业务费用调用
    loadChargeWithRule() {
      if (!this.ruleItem) {
        return;
      }
     if (!this.ruleItem.ruleParams) {
       return;
     }
      let ruleData = JSON.parse(this.ruleItem.ruleParams);
      ruleData.itemPrice = this.ruleItem.itemPrice;
      ruleData.appointmentInTime = dataFormate(
        new Date(this.copyFareWellInfo.farewellMourn.appointmentInTime)
      );
      ruleData.appointmentOutTime = dataFormate(
        new Date(this.copyFareWellInfo.farewellMourn.appointmentOutTime)
      );
      let startTimeLong = new Date(
        this.copyFareWellInfo.farewellMourn.appointmentInTime
      ).getTime();
      let endTimeLong = new Date(
        this.copyFareWellInfo.farewellMourn.appointmentOutTime
      ).getTime();

      if (startTimeLong >= endTimeLong) {
        this.copyFareWellInfo.farewellMourn.appointmentOutTime = "";
        this.$message("结束时间不能小于等于开始时间!.");
        return;
      }
      let postData = {
        ruleCode: this.ruleItem.ruleCode,
        ruleData: JSON.stringify(ruleData),
      };
      Type["loadChargeWithRule"](postData)
        .then((response) => {
          let result = response;
          const { data } = result;
          if (!data) {
            this.$message("费用计算信息出错.");
          }

          this.copyFareWellInfo.farewellMourn.charges.businessCharge.itemCharge =
            data.itemCharge;
          //规则编码
          this.copyFareWellInfo.farewellMourn.ruleCode = data.ruleCode;
          //规则数量
          this.copyFareWellInfo.farewellMourn.ruleNumber = data.ruleNumber;
          //计量单位
          this.copyFareWellInfo.farewellMourn.ruleUnit = data.ruleUnit;

          //规则描述
          this.copyFareWellInfo.farewellMourn.ruleRemark = data.ruleRemark;
          this.$forceUpdate();

          //数据读取成功
        })
        .catch((error) => {
          this.$message(error);
        });
    },
    //级联选择
    handleChange(value) {},
    //告别厅保存
    Save() {
      if (Date.now() - this.onpresscTime > 2000) {
        this.onpresscTime = Date.now();
        console.log(Date.now());
      } else {
        return;
      }
      let hallTypeCode =
        typeof this.copyFareWellInfo.farewellMourn.hallTypeCode == "number" &&
        this.copyFareWellInfo.farewellMourn.hallTypeCode >= 0;
      let appointmentInTime = this.copyFareWellInfo.farewellMourn
        .appointmentInTime;

      if (!hallTypeCode || !appointmentInTime || !this.selectSave.hallCode) {
        this.$message("信息未填写完整");
        return;
      }
      let obj = JSON.parse(JSON.stringify(this.copyFareWellInfo.farewellMourn));
      obj.charges.goodsServices.forEach((item, index) => {
        if (item.asSettled == 1) {
          obj.charges.goodsServices.splice(index, 1);
        }
      });
      if (this.FareWellAddFlag) {
        obj.id = 0;
        obj.hallTypeCode = obj.hallTypeCode;
        obj.hallId = obj.hallId;
        obj.appointmentInTime = dataFormate(new Date(obj.appointmentInTime));
        obj.appointmentOutTime = dataFormate(new Date(obj.appointmentOutTime));
        obj.realityInTime = "";
        obj.realityOutTime = "";
        obj.randomCode = "";
        obj.chargeId = "";
        obj.businessType = "farewell_mourn_type";
        obj.version = 0;
        obj.charges.businessCharge.id = 0;
        obj.charges.businessCharge.discountCharge = 0;
        obj.charges.businessCharge.benefitCharge = 0;
        obj.charges.businessCharge.asSettled = 0;
        obj.charges.businessCharge.chargeSettleId = 0;
        obj.charges.businessCharge.randomCode = 0;
        obj.charges.businessCharge.version = 0;
        obj.charges.businessCharge.chargeTime = "";
        obj.charges.businessCharge.businessCode = obj.businessCode;
        obj.charges.businessCharge.itemCharge = toFixed(
          Number(
            this.copyFareWellInfo.farewellMourn.charges.businessCharge
              .itemCharge
          ),
          2
        );
        obj.charges.businessCharge.realityCharge = toFixed(
          Number(
            this.copyFareWellInfo.farewellMourn.charges.businessCharge
              .itemCharge
          ),
          2
        );
      }
      if (!this.FareWellAddFlag) {
        obj.appointmentInTime = dataFormate(new Date(obj.appointmentInTime));
        obj.appointmentOutTime = dataFormate(new Date(obj.appointmentOutTime));
      }

      if (obj.charges.goodsServices.length > 0) {
        obj.charges.goodsServices.forEach((item, index) => {
          // item.businessCode = obj.businessCode;
          item.itemPrice = toFixed(item.itemPrice, 2);
          item.itemNumber = toFixed(item.itemNumber, 2);
          item.discountCharge = toFixed(item.discountCharge, 2);
          item.itemCharge = toFixed(item.itemCharge, 2);
        });
      }
      let postData = obj;
      // console.log(obj)
      this.saveFlag = true;
      mournService["saveFarewellMourn"](postData)
        .then((response) => {
          let result = response;
          const { data } = result;
          if (!data) {
            this.saveFlag = false;
            this.$message("数据保存失败");
            return;
          }
          //数据读取成功
          this.copyFareWellInfo.farewellMourn = data;
          this.saveFlag = false;
          this.$message("保存数据成功");
          this.$parent.DialogFareWell = false;
        })
        .catch((error) => {
          this.$message(error);
          this.saveFlag = false;
        });
    },

    //追加告别服务明细
    AddRow(index) {
      let row = this.copyGoodServices[index];
      if (index >= 0 && row) {
        row.itemPrice = Number(row.itemPrice).toFixed(2);
        row.itemNumber = Number(row.itemNumber).toFixed(2);
        row.itemCharge = Number(row.itemPrice * row.itemNumber).toFixed(2); //计算项目金额
        row.discountCharge = Number(row.discountCharge).toFixed(2);
        let numberValidate =
          Number(row.itemPrice) >= 0 &&
          Number(row.itemNumber) >= 0 &&
          Number(row.discountCharge) >= 0;
        if (
          !numberValidate ||
          !Number(row.itemCharge) ||
          Number(row.itemCharge) < Number(row.discountCharge)
        ) {
          this.$message("数值错误，请重新填写");
          Object.assign(
            row,
            this.copyFareWellInfo.serviceItems.use_in_mourn_ceremony[index]
          );
          return true;
        }
        // 减免金额
        row.benefitCharge = 0;
        row.asSettled = 0;
        //子项目的serviceItemId=id;
        row.serviceItemId = row.id;
        //新增的id为0
        row.id = 0;

        let list = JSON.parse(JSON.stringify(this.copyGoodServices));
        this.copyFareWellInfo.farewellMourn.charges.goodsServices.push(
          list[index]
        );
        Object.assign(
          row,
          this.copyFareWellInfo.serviceItems.use_in_mourn_ceremony[index]
        );
      }
    },

    deleteRow(index, arry) {
      let this_delete = this;
      //定义要删除的数据
      let item = {
        id: arry[index].id,
        version: arry[index].version,
      };
      //先添加删除，后再删除服务物品
      let deleteItems =
        this_delete.copyFareWellInfo.farewellMourn.charges.deleteIds || [];
      if (arry[index] && item.id != 0) {
        //放入删除数据中
        deleteItems.push(item);
        //放入要要删除的数组中
        this_delete.copyFareWellInfo.farewellMourn.charges.deleteIds = deleteItems;
      }

      //删除指定数据
      this_delete.copyFareWellInfo.farewellMourn.charges.goodsServices.splice(
        index,
        1
      );
    },
    mournSelect(item, index) {
      //在弹出选择礼厅对话框中选择
      if (item.deadName && item.businessId > 0) {
        this.$message("礼厅被占用,请选择其他");
        return;
      }
      if (item.disableState == 1) {
        this.$message("礼厅为停用状态");
        return;
      }
      for (let j = 0; j < this.farewellHalls.length; j++) {
        this.farewellHalls[j].flag = false;
      }
      this.farewellHalls[index].flag = !this.farewellHalls[index].flag;
      this.$set(this.farewellHalls, index, this.farewellHalls[index]);
      this.selectname = this.farewellHalls[index];
    },
    hallTypechange(id) {
      //加载礼厅名称
      let startTime = this.copyFareWellInfo.farewellMourn.appointmentInTime;
      startTime = dataFormate(
        new Date(this.copyFareWellInfo.farewellMourn.appointmentInTime)
      );
      if (startTime && startTime.indexOf("/") != -1) {
        startTime = startTime.replace(/\//g, "-");
      }
      let endTime = this.copyFareWellInfo.farewellMourn.appointmentOutTime;
      endTime = dataFormate(
        new Date(this.copyFareWellInfo.farewellMourn.appointmentOutTime)
      );
      if (startTime && startTime.indexOf("/") != -1) {
        startTime = startTime.replace(/\//g, "-");
      }
      if (endTime && endTime.indexOf("/") != -1) {
        endTime = endTime.replace(/\//g, "-");
      }
      let postData = {
        hallTypeCode: this.copyFareWellInfo.farewellMourn.hallTypeCode, //选中id
        appointmentInTime: startTime,
        appointmentOutTime:endTime,
      };
      mournService["loadFarewellHallWithDate"](postData)
        .then((response) => {
          let result = response;
          const { data } = result;
          if (!data) {
            this.$message("数据加载失败");
            return;
          }
          //数据读取成功

          this.farewellHalls = data;
           console.log(data);

          //测试用
          this.centerDialogVisible = true;

          console.log(this.farewellHalls);
        })
        .catch((error) => {
          this.$message(error);
        });
    },
    //显示选择告别厅对话框
    select() {
      // if (!this.copyFareWellInfo.farewellMourn.hallTypeCode || !this.copyFareWellInfo.farewellMourn.appointmentInTime) {
      //     this.$message("请先选择告别厅及开始时间")
      //     return
      // }

      if (!this.copyFareWellInfo.farewellMourn.appointmentInTime) {
        this.$message("请先选择开始时间");
        return;
      }
      if (this.ProcessStateFareWellOld == 3) {
        this.$message("已出厅，状态锁定");
        return;
      }
      if (this.copyFareWellInfo.farewellMourn.charges.businessCharge.asSettled==1){
        this.$message("已结算，状态锁定");
        return;
      }
      //判断礼厅是否已禁用
      if (this.ruleItems && this.ruleItems.length > 0) {
        for (let i = 0; i < this.ruleItems.length; i++) {
          let item = this.ruleItems[i];
          if (
            item &&
            item.dataCode == this.copyFareWellInfo.farewellMourn.hallTypeCode &&
            item.disableState
          ) {
            this.$message("礼厅类型已禁用");
            return;
          }
        }
      }
      this.hallTypechange(this.copyFareWellInfo.farewellMourn.hallTypeCode);
    },//改变规则说明
    changRemark(v){
    let str=this.copyFareWellInfo.farewellMourn.ruleRemark;
    let indexStart=str.indexOf('【');
    let indexEnd=str.indexOf('】');
    let strNew=str.substring(0,indexStart+1)+v+str.substring(indexEnd);
    this.copyFareWellInfo.farewellMourn.ruleRemark=strNew;
    //强制更新
    this.$forceUpdate()
  }, //判断状态回退
    changeFareWellProcessStateCode(code) {
      this.$forceUpdate();
    },
    showSelectName() {
      //选择礼厅名称
      this.selectSave = JSON.parse(JSON.stringify(this.selectname));
      this.copyFareWellInfo.farewellMourn.hallId = this.selectname.id;
      // this.copyFareWellInfo.farewellMourn.processStateCode = ""; //不能改变礼厅状态
       console.log(this.selectSave.hallType+"uuuuuuuuuuuuuuuuuuuuuuuuu");
        console.log(this.selectSave.hallCode+"uuuuuuuuuuuuuuuuuuuuuuuuu")
        console.log(this.selectSave.hallName+"uuuuuuuuuuuuuuuuuuuuuuuuu")
      // console.log(this.ruleItems);
      // 通过选择礼厅名称选择告别厅类型
      let x = this.ruleItems.find((i) => {
        if (i.dataText === this.selectSave.hallType) {
          return i;
        }
      });
      console.log(this.ruleItems)
      console.log(x)
      console.log(this.selectSave.hallType)

      this.copyFareWellInfo.farewellMourn.hallTypeCode = x.dataCode;
      this.changeTypeCodeAsRuleItem(x.dataCode);

      this.centerDialogVisible = false;
    },
    closeDialog() {
      this.$parent.DialogFareWell = false;
    },
    changeOutTime(){
      this.copyFareWellInfo.farewellMourn.appointmentOutTime= new Date(this.copyFareWellInfo.farewellMourn.appointmentInTime);
      this.copyFareWellInfo.farewellMourn.appointmentOutTime.setMinutes(this.copyFareWellInfo.farewellMourn.appointmentOutTime.getMinutes()+30);
    },
  },
};
</script>

<style>
.FareWellDialog .el-dialog__title,
.FareWellDialog .el-dialog__headerbtn .el-dialog__close {
  color: white;
}

.FareWellDialog .el-input,
.FareWellDialog .el-select,
.FareWellDialog .el-form-item__content {
  width: 180px;
}

.FareWellDialog .el-radio.is-bordered + .el-radio.is-bordered {
  margin-left: 0px;
}

.FareWellDialog .el-card__body {
  padding: 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.FareWellDialog .el-radio--medium.is-bordered {
  padding: 10px 20px 0 10px;
  border-radius: 4px;
  height: 36px;
  width: 120px;
  /*flex: 0 0 120px;*/
  margin-bottom: 20px;
}

.FareWellDialog .el-radio {
  margin-right: 0px;
}
</style>
<style scoped>
.wrapper {
  display: flex;
  flex-wrap: wrap;
  margin: 5px 0px;
  justify-content: space-between;
}

.box-card {
  flex: 0 0 220px;
  display: inline-block;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
  margin-right: 10px;
}

.box-card .item {
  font-size: 16px;
  font-weight: 400;
  line-height: 30px;
  width: 100%;
  height: 150px;
  text-align: left;
  padding: 0 1em;
  border-radius: 5px;
  overflow: hidden;
}

.item:hover {
  cursor: pointer;
}

.item:last-child {
  margin-bottom: 0px;
}

.item.sale {
  background-color: #ff6040;
}

.item.free {
  background: white;
}

.item.repair {
  background: yellow;
}

.item.selected {
  background: lightgreen !important;
}

.item h2 {
  height: 30px;
  overflow: hidden;
  width: 220px;
}
</style>

